package com.lizardmind.haofudoctor.ui.diagnosis.sickMan.advisory

import android.databinding.ViewDataBinding
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.util.getDataBinding
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.databinding.ActivityBianZhengKaiFangBinding
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.MedicalHistoryBean
import com.lizardmind.haofudoctor.other.Request
import com.lizardmind.haofudoctor.ui.diagnosis.diagnose.LookDiagnoseActivity
import com.lizardmind.haofudoctor.ui.diagnosis.sickMan.MedicalHistory.MedicalHistoryAdapter
import com.lizardmind.haofudoctor.ui.diagnosis.visit.LookVisitActivity
import io.reactivex.functions.Action
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_bian_zheng_kai_fang.*
import kotlinx.android.synthetic.main.include_title_text.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

/**
 * 辩证开方activity
 */
class BianZhengKaiFangActivity : BaseActivity() {
    val list= arrayListOf<MedicalHistoryBean>()
    val uid by lazy { intent.getStringExtra("user_id") }

    val adapter by lazy { MedicalHistoryAdapter(list,this) }
    val binding by lazy { getDataBinding<ActivityBianZhengKaiFangBinding>(R.layout.activity_bian_zheng_kai_fang) }
    override fun onCreatRootView(): View? {
        return binding.root
    }

    override fun onInitView(savedInstanceState: Bundle?) {
        titleText.text="辩证开方"
        imgLeft.setImageResource(R.mipmap.back)
        imgLeft.onClick { finish() }

        rvBianZhengKaiFang.apply {
            layoutManager=LinearLayoutManager(this@BianZhengKaiFangActivity)
            adapter=this@BianZhengKaiFangActivity.adapter
        }

        refreshLayout.setEnableLoadmore(false)
        refreshLayout.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                super.onRefresh(refreshLayout)
                list.clear()
                getData()
            }
        })

        refreshLayout.startRefresh()

        adapter.setOnBind(object :MedicalHistoryAdapter.OnBind{
            override fun onBind(itemBinding: ViewDataBinding, position: Int,bean: MedicalHistoryBean) {
                itemBinding.root.onClick {
                    when(bean.itemFlag){
                        1->{
                            startActivity<LookDiagnoseActivity>("id" to bean.id)
                        }
                        2->{
                            startActivity<LookVisitActivity>("id" to bean.id)
                        }
                    }
                }
            }
        })
    }

    private fun getData(){
        observe {
            postApi<Request<List<MedicalHistoryBean>>>(hashMapOf(
                    "action" to "prescribing_list",
                    "token" to DoctorInfo.token,
//                    "uid" to "1"//uid=====================测试写死的====================
                    "uid" to uid
            ), Consumer {
                it.dispose {
                    list.addAll(it)
                    adapter.notifyDataSetChanged()
                }
            }, Action {
                refreshLayout.finishRefreshing()
            })
        }
    }

}
